<?
class Controller {
    public $_methods = null;
    public $_uriSegments = null;
    private $_uri;

    function __construct($uri = null) {
        $this->_methods = get_class_methods($this);
        $this->_uriSegments = isset($uri) ?  explode("/", trim($uri,"/")) : array();;
        $this->_uri = $uri;
        $this->initController();
    }

    protected function initController() {}

    public function isValidAction($action) {
        if(!in_array($action, $this->_methods)) return false; 
        if(!is_callable(array($this, $action))) return false;
        return true;
    }
    public static function factory($controllerName, $uri = null) {
        // Los controladores se ubican en ./controllers. 
        $controllerClassName = ucfirst($controllerName)."Controller";
        if (!file_exists("controllers/". $controllerClassName . ".php")) return false;

        include "controllers/". $controllerClassName . ".php";

        if(! class_exists($controllerClassName)) return false;
        $controller = new $controllerClassName($uri);
        if(!$controller instanceof Controller) return false;
        return $controller;
    }

}
?>
